//
//  CZStatusOriginalView.swift
//  iOSWeibo10
//
//  Created by ge on 16/5/16.
//  Copyright © 2016年 ge. All rights reserved.
//

import UIKit
import SDWebImage


/// 原创微博view
class CZStatusOriginalView: UIView {
    
    /// 原创微博view根据模型来显示内容, 原创微博view的微博模型,是cell来设置,当cell有模型的时候来设置
    var status: CZStatus? {
        didSet {
            // 3.cell给原创view设置了模型,有模型后可以给原创view的子控件设置内容
            
            // 头像
            if let uslString = status?.user?.profile_image_url {
                let url = NSURL(string: uslString)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            // 名称
            let name = status?.user?.screen_name
            nameLabel.text = name
            
            //时间
            timeLabel.text = status?.created_at
            
            //来源
            sourceLabel.text = status?.source
            
            // 会员等级图认证类型图片,在view里面来判断在设置是不合适的,如果在其他view里面也要设置会员等级和认证类型,又需要再判断,会出现重复代码,最好是让模型直接提供一张图片,view只要设置图片就可以
            //会员等级
            memberView.image = status?.user?.mbrankImage
            
            //认证类型
            verifiedView.image = status?.user?.verifiedImage
            
            // 微博内容
            contentLabel.text = status?.text
            
            // 设置配图视图的URL数组, 配图里面会自己更新大小
            pictureView.pictureURLs = status?.pictureURLs
            
            // 配图有图片显示,配图的顶部在contentLabel的底部,偏移8
            // 配图没有图片显示,配图的顶部在contentLabel的底部,不要偏移
            pictureView.snp_updateConstraints { (make) -> Void in
                let margin = status?.pictureURLs?.count == 0 ? 0 : CellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    private func prepareUI() {
        backgroundColor = UIColor.whiteColor()
        
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 会员等级图标
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 微博文本内容label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        /// 微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            // 先随便写一个size.后期配图根据里面的图片数量自己重新设置大小
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        /// 设置原创view的底部和contentLabel的底部重合偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
    }
    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        // 设置label大小
        label.font = UIFont.systemFontOfSize(NameFontSize)
        
        // 设置颜色
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        // 设置label大小
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        
        // 设置颜色
        label.textColor = UIColor.orangeColor()
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        // 设置label大小
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        
        // 设置颜色
        label.textColor = UIColor.lightGrayColor()
        
        return label
    }()
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    
    /// 认证类型
    private lazy var verifiedView: UIImageView = UIImageView(image: nil)
    
    /// 微博文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // label大小
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        
        // 颜色
        label.textColor = UIColor.darkGrayColor()
        
        // 多行
        label.numberOfLines = 0
        
        // 最大换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * CellMargin
        
        return label
    }()
    
    /// 微博配图
    private lazy var pictureView: CZStatusPictureView = CZStatusPictureView()
}











